package com.jinlejia.erp.ui.customer;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.freedom.yefeng.yfrecyclerview.YfListMode;
import com.jinlejia.erp.R;
import com.jinlejia.erp.adapter.DistrictAdapter;
import com.jinlejia.erp.bean.EstateBean;
import com.jinlejia.erp.databinding.ActivityInterestDistrictBinding;
import com.jinlejia.erp.ui.common.CommonDeleteDialog;
import com.jinlejia.erp.ui.house.SearchDistrictActivity;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 录客源兴趣小区
 */
public class RecordCusromerInterestActivity extends AppCompatActivity {

    DistrictAdapter adapter;
    private ArrayList<String> estateList= new ArrayList<>();
    private ArrayList<String> insertList= new ArrayList<>();
    private ArrayList<String> delList= new ArrayList<>();

    private ActivityInterestDistrictBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_interest_district);
        binding.btBack.setOnClickListener(this::onClick);
        binding.btAdd.setOnClickListener(this::onClick);

        EventBus.getDefault().register(this);
        estateList = getIntent().getStringArrayListExtra("estateList");

        binding.yfRecycler.setHasFixedSize(true);
        binding.yfRecycler.setLayoutManager(new LinearLayoutManager(this));
        ArrayList<Map<String, Object>> list = new ArrayList();
        for( String name : estateList ){
            Map<String, Object> map = new HashMap<>();
            map.put("estateName",name);
            list.add(map);
        }
        adapter = new DistrictAdapter(list);
        binding.yfRecycler.setAdapter(adapter);

        adapter.setOnDeleteListiner((i, name) -> {
            CommonDeleteDialog dialog = new CommonDeleteDialog(RecordCusromerInterestActivity.this, "删除", "确定删除" + name + "?");
            dialog.show();
            dialog.setConfirmListener(() -> remove(i, name));
        });

    }



    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_back:
                Intent data = new Intent();
                data.putExtra("data",estateList);
                data.putExtra("insert",insertList);
                data.putExtra("del",delList);
                setResult(RESULT_OK,data);
                finish();
                break;
            case R.id.bt_add:
                //添加意向小区
                Intent intent = new Intent(this,SearchDistrictActivity.class);
                intent.putExtra("csCode","");
                intent.putExtra("type",getIntent().getStringExtra("type"));
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    private void remove(final int p, String name) {
        adapter.getData().remove(p);
        adapter.notifyItemRemoved(p);
        adapter.notifyItemRangeChanged(0,adapter.getItemCount());
        estateList.remove(name);
        delList.add(name);
        if (adapter.mData.size() == 0)
            adapter.changeMode(YfListMode.MODE_EMPTY);
    }
    @Subscriber( tag = "TAG_INTEREST_DISTRICT" )
    public void interest( ArrayList data ){
        ArrayList<Map<String,Object>> list = new ArrayList<>();
        for( int i=0;i<data.size();i++ ){
            Map<String,Object> map = new HashMap<>();
            EstateBean icb = (EstateBean) data.get(i);
            if( !estateList.contains(icb.getEstateName()) ) {
                map.put("estateName", icb.getEstateName());
                map.put("areaName", icb.getArea());
                map.put("countyName", icb.getCounty());
                list.add(map);
                estateList.add(icb.getEstateName());
                insertList.add(icb.getEstateName());
            }
        }
        if(adapter.getData().size()>0 ) {
            adapter.addData(list);
        }else{
            adapter.setData(list);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Intent data = new Intent();
        data.putExtra("data",estateList);
        data.putExtra("insert",insertList);
        data.putExtra("del",delList);
        setResult(RESULT_OK,data);
        return super.onKeyDown(keyCode, event);
    }

}
